import random
import pygame,sys
import time
chang=4
gao=3
block=200
kong=None


def draw(screen,image_surface,chang,gao,b):
    x=[] 
    for i in range(chang):
        y=[]
        for j in range(gao):
            rect1 = pygame.Rect(i*b,j*b,b,b)
            y.append(rect1)
            image_child= image_surface.subsurface(rect1)
            rect2 = image_child.get_rect()
            screen.blit(image_child,rect1)
            pygame.display.flip()
            time.sleep(0.05)
        x.append(y[:])
        print(x)
    return x

def dong(arr,x,y,image_surface,screen):
    global kong
    global block
    b=block
    if x-1>=0:
        if arr[x-1][y]==None:
            #向左
            arr[x-1][y]=arr[x][y]
            arr[x][y]=None
    elif x+1<=chang-1:
        if arr[x+1][y]==None:
            #向右
            arr[x+1][y]=arr[x][y]
            arr[x][y]=None
    elif y-1>=0:
        if arr[x][y-1]==None:
            #向上
            arr[x][y-1]=arr[x][y]
            arr[x][y]=None
    elif y+1<=gao-1:
        if arr[x][y+1]==None:
            #向下
            arr[x][y+1]=arr[x][y]
            arr[x][y]=None
    elif x==2 and y==3 and kong==None:
        kong=arr[x][y]
        arr[x][y]=None
    elif x==3 and y==3 and arr[x][y-1]==None:
        arr[x][y-1]=kong
        kong=None
    
    for i in range(0,chang):
        for j in range(0,gao):
            if arr[i][j]==None:
                pxgame.draw.rect(screen,[255,255,255],[i*b,j*b,b,b],0)
            else:
                rect1 = arr[i][j]
                image_child= image_surface.subsurface(rect1)
                rect2 = image_child.get_rect()
                screen.blit(image_child,rect1)

    if kong==None:
        pygame.draw.rect(screen,[255,255,255],[3*b,3*b,b,b],0)
    else:
        rect1 = kong
        image_child= image_surface.subsurface(rect1)
        rect2 = image_child.get_rect()
        screen.blit(image_child,rect1)
    pygame.display.flip()

def main():
    pygame.init()
    screen = pygame.display.set_mode((chang*block,(gao+1)*block))
    screen.fill([255,255,255])
    pygame.display.set_caption('拼图')
    
    img_wh=[chang*block,gao*block]
    image_surface = pygame.image.load("python3/study/03.pintu/bg01.jpg.").convert()
    image_surface = pygame.image.load("python3/study/03.pintu/bg01.jpg").convert()
    #image_surface = pygame.image.load("./bg01.jpg").convert()
    image_surface=pygame.transform.scale(image_surface,img_wh)
    arr=draw(screen,image_surface,chang,gao,block)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button==1:
                    x,y=event.pos
                    x=x//block 
                    y=y//block
                    dong(arr,x,y,image_surface,screen)
        #pygame.display.update()

main()

















"""
g=[]
for i in range(1,16):
    g.append(i)
x=[]
z=[]
i=1
while i < 16:
    ii=random.randint(1,15)
    if ii in g:
        g.remove(ii)
        x.append(ii)
        z.append(1)
        i+=1
z.append(0)
x.append(0)
print(x,z)
score=100



while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    pygame.display.flip()






def draw(x,z):
    for i in range(0,4):
        for ii in range(0,4):
            my_score = font.render(str(x[ii]), False, (255, 255, 0))


font = pg.font.Font(None, 60)
draw(x,z)
pygame.init()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    pygame.display.flip()
00
#创建窗口，设置大小
  # 2.获得字体文件
  # 1.获取font对象（需要字体文件）# 绘制内容：text为内容，True为是否抗锯齿， WHITE是字体颜色

game_window = pg.display.set_mode((600, 600))
font = pg.font.Font(None, 60)
my_score = font.render(str(score), False, (255, 255, 0))
game_window.blit(my_score, (1, 1))
"""
